package com.xin.wx;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;

import java.util.HashMap;
import java.util.Map;

import javax.lang.model.element.Modifier;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.Elements;

/**
 * 生成微信 登录的分享的
 */
public class WxSocialOptUtils {

    /*IWXAPI iwxapi;*/
    public FieldSpec getIWXAPI() {
        ClassName type = ClassName.get("com.tencent.mm.opensdk.openapi", "IWXAPI");
        FieldSpec.Builder builder = FieldSpec.builder(type, "iwxapi")
                .addModifiers(Modifier.PRIVATE);
        return builder.build();
    }

    /*  @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          iwxapi = WXAPIFactory.createWXAPI(this, Constant.WX_AppID);
          iwxapi.handleIntent(getIntent(), this);
      }*/
    public MethodSpec getOnCreate() {
        MethodSpec.Builder builder = MethodSpec.methodBuilder("onCreate")
                .addModifiers(Modifier.PROTECTED)
                .addParameter(ClassName.get("android.os", "Bundle"), "savedInstanceState")
                .addStatement("super.onCreate(savedInstanceState)")
                .addStatement("iwxapi = $T.createWXAPI(this, $T.WX_AppID)",
                        ClassName.get("com.tencent.mm.opensdk.openapi","WXAPIFactory"),
                        ClassName.get("com.xin.social.share", "Constant"))
                .addStatement("iwxapi.handleIntent(getIntent(), this)")
                .addAnnotation(Override.class);

        return builder.build();
    }

    /*@Override
    public void onReq(BaseReq baseReq) {

    }*/
    public MethodSpec getOnReq() {
        MethodSpec.Builder builder = MethodSpec.methodBuilder("onReq")
                .addModifiers(Modifier.PUBLIC)
                .addParameter(ClassName.get("com.tencent.mm.opensdk.modelbase", "BaseReq"), "resp")
                .addAnnotation(Override.class);
        return builder.build();
    }

    /*   @Override
    public void onResp(BaseResp resp) {
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                onSuccess(resp);
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                onCancel(resp);
                break;
            default:
                onFail(resp);
                break;
        }
    }*/
    public MethodSpec getOnResp() {
        MethodSpec.Builder builder = MethodSpec.methodBuilder("onResp")
                .addModifiers(Modifier.PUBLIC)
                .addParameter(ClassName.get("com.tencent.mm.opensdk.modelbase", "BaseResp"), "resp")
                .addAnnotation(Override.class)
                .beginControlFlow(" switch (resp.errCode) ")
                .addCode("case BaseResp.ErrCode.ERR_OK:\n")
                .addStatement("onSuccess(resp)")
                .addStatement("break")
                .addCode("case BaseResp.ErrCode.ERR_USER_CANCEL:\n")
                .addStatement("onCancel(resp)")
                .addStatement("break")
                .addCode("default:\n")
                .addStatement("onFail(resp)")
                .addStatement("break")
                .endControlFlow();
        return builder.build();
    }

    /* private void onSuccess(BaseResp resp) {
        switch (resp.getType()) {
            case ConstantsAPI.COMMAND_SENDAUTH:
                String code = ((SendAuth.Resp) resp).code;
                log("code = " + code);
                if (LoginUtils.getListener() != null) {
                    LoginUtils.getListener().onWx(code);
                }
                finish();
                break;

            case ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX:
                if (ShareUtils.shareCallBack != null) {
                    ShareUtils.shareCallBack.onComplete();
                } else {
                    showToast("微信分享成功");
                }
                finish();
                break;
        }
    }*/
    public MethodSpec getOnSuccess() {
        MethodSpec.Builder builder = MethodSpec.methodBuilder("onSuccess")
                .addParameter(ClassName.get("com.tencent.mm.opensdk.modelbase", "BaseResp"), "resp")
                .beginControlFlow("switch (resp.getType()) ")
                .addCode("case ConstantsAPI.COMMAND_SENDAUTH:\n")
                .addStatement("String code = (($T.Resp) resp).code",ClassName.get("com.tencent.mm.opensdk.modelmsg","SendAuth"))
                .beginControlFlow("if (LoginUtils.getListener() != null) ")
                .addStatement("$T.getListener().onWx(code)",ClassName.get("com.xin.social.login","LoginUtils"))
                .endControlFlow()
                .addStatement("finish()")
                .addStatement("break")
                .addCode("case ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX:\n")
                .beginControlFlow("if (ShareUtils.shareCallBack != null) ")
                .addStatement("ShareUtils.shareCallBack.onComplete()")
                .nextControlFlow("else ")
                .addStatement("showToast($T.WxShareSuccess)", ClassName.get("com.xin.social.share","Constant"))
                .endControlFlow()
                .addStatement("finish()")
                .addStatement("break")
                .endControlFlow();
        return builder.build();
    }

    /* private void onCancel(BaseResp resp) {
        if (ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX == resp.getType()) {
            showToast("分享取消");
            if (ShareUtils.shareCallBack != null) {
                ShareUtils.shareCallBack.onCancel();
            }
        } else {
            showToast("登录取消");
        }
        finish();
    }
    */
    public MethodSpec getOnCancel() {
        MethodSpec.Builder builder = MethodSpec.methodBuilder("onCancel")
                .addParameter(ClassName.get("com.tencent.mm.opensdk.modelbase", "BaseResp"), "resp")
                .beginControlFlow("if($T.COMMAND_SENDMESSAGE_TO_WX == resp.getType())", ClassName.get("com.tencent.mm.opensdk.constants", "ConstantsAPI"))
                .addStatement("showToast($T.WxShareCancel)",  ClassName.get("com.xin.social.share","Constant"))
                .addStatement("if ($T.shareCallBack != null) {", ClassName.get("com.xin.social.share", "ShareUtils"))
                .addStatement("ShareUtils.shareCallBack.onCancel();")
                .addStatement("}")
                .nextControlFlow("showToast($T.WxLoginCancel);",  ClassName.get("com.xin.social.share","Constant"))
                .endControlFlow()
                .addStatement(" finish()");
        return builder.build();
    }

    /* private void onFail(BaseResp resp) {
        if (ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX == resp.getType()) {
            showToast("分享失败");
            if (ShareUtils.shareCallBack != null) {
                ShareUtils.shareCallBack.onError();
            }
        } else {
            showToast("登录失败");
        }
        finish();
    }*/
    public MethodSpec getOnFail() {
        MethodSpec.Builder builder = MethodSpec.methodBuilder("onFail")
                .addParameter(ClassName.get("com.tencent.mm.opensdk.modelbase", "BaseResp"), "resp")
                .beginControlFlow("if($T.COMMAND_SENDMESSAGE_TO_WX == resp.getType())", ClassName.get("com.tencent.mm.opensdk.constants", "ConstantsAPI"))
                .addStatement("showToast($T.WxShareFail)", ClassName.get("com.xin.social.share","Constant"))
                .addStatement("if ($T.shareCallBack != null) {", ClassName.get("com.xin.social.share", "ShareUtils"))
                .addStatement("ShareUtils.shareCallBack.onError();")
                .addStatement("}")
                .nextControlFlow("showToast($T.WxLoginFail);",  ClassName.get("com.xin.social.share","Constant"))
                .endControlFlow()
                .addStatement(" finish()");
        return builder.build();
    }

    /* private void log(String errStr) {
        Log.e(getClass().getSimpleName(), errStr);
    }*/
    public MethodSpec getLog() {
        MethodSpec.Builder builder = MethodSpec.methodBuilder("log")
                .addParameter(String.class, "str")
                .addStatement("$T.e(getClass().getSimpleName(), str)", ClassName.get("android.util", "Log"));
        return builder.build();
    }

    /*private void showToast(String str) {
       Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }*/
    public MethodSpec getShowToast() {
        MethodSpec.Builder builder = MethodSpec.methodBuilder("showToast")
                .addParameter(String.class, "str")
                .addStatement("$T.makeText(this, str, Toast.LENGTH_SHORT).show()", ClassName.get("android.widget", "Toast"));
        return builder.build();
    }

}
